package com.bawei.nbluetoothdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedHelpers;

import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private BluetoothAdapter adapter;
    private MyAdapter mMyAdapter;
    private ListView lv;
    private MyRecevier myRecevier;
    private List<BluetoothDevice> list2 = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    "android.permission.ACCESS_COARSE_LOCATION",
                    "android.permission.ACCESS_FINE_LOCATION",
                    "android.permission.BLUETOOTH_ADMIN",
                    "android.permission.BLUETOOTH",
                    "android.permission.BLUETOOTH_CONNECT"

            }, 101);
        }


        initView();
        //获得本机蓝牙+注册广播和解除注册
        initBlueTooth();


    }
    private void initBlueTooth() {
        BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);//蓝牙管理者
        adapter = manager.getAdapter();
        myRecevier=new MyRecevier();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//扫描到远程设备
        registerReceiver(myRecevier,intentFilter);
    }

    //hook蓝牙的扫描
    private void hookBlueTooth() {
        XposedHelpers.findAndHookMethod(android.bluetooth.BluetoothAdapter.class, "startDiscovery", new XC_MethodReplacement() {
            @Override
            protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
                Log.d("ytx", "有人扫描附近的蓝牙设备:禁止 ");
                return null;
            }
        });
    }

    private void initView() {
        lv = findViewById(R.id.lv);
        mMyAdapter = new MyAdapter(list2,this);
        lv.setAdapter(mMyAdapter);

    }

    //扫描附近的蓝牙设备
    @SuppressLint("MissingPermission")
    public void show(View view) {
        adapter.startDiscovery();
    }
    //TODO 1:广播接收者：接受系统发送的广播，扫描到设备
    class MyRecevier extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){//扫描到一个
                //获得远程蓝牙设备
                BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //放到listview中
                list2.add(device);
                mMyAdapter.notifyDataSetChanged();
            }
        }
    }

}